package com.feixiu.mapreduce.javaserilizable;

import org.junit.Test;

import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Serilize {

    /**
     * 进行序列化           序列化的对象  需要实现Serilizable或者Externalizable接口
     * 1.获取ObjectOutputStream
     * 2.调用writeObject函数
     * notes:如果成员变量是   引用类型，则该成员变量也需要是 继承Serilizable或者Externalizable接口
     * @throws ParseException
     */
    @Test
    public void serilizePerson() throws ParseException {
        String  birthSee="1999-04-23 19:23:11";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date birth = simpleDateFormat.parse(birthSee);
        long birthTime = birth.getTime();
        String name="丁昌耀";
        int age=22;
        double height=1.75;
        Person person = new Person(name,age,height,birthTime,birthSee);//需要实现序列化
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\Desktop\\temp\\person.txt"))){
            //获取ObjectOutPutStream   输出流
            oos.writeObject(person);//把对象序列化到文件
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 反序列化
     * 1.创建一个ObjectInputStream 输入流
     * 2.调用ObjectInputStream 对象   的readObject()得到序列化的对象
   Ob* @throws ParseException
     */
    @Test
    public void getSerilizePerson() throws ParseException {


        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\Desktop\\temp\\person.txt"))){
            //ObjectInputStream   输出流
            Person person = (Person) ois.readObject();
            System.out.println(person);

        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
